//
//  LoginViewController.m
//  StandardizedProduct
//
//  Created by 宁宁 on 16/6/20.
//  Copyright © 2016年 ym. All rights reserved.
//

#import "LoginViewController.h"

#import "RegisterViewController.h"//注册界面



@interface LoginViewController ()

@property (weak, nonatomic) IBOutlet UITextField *password;//密码

@end

@implementation LoginViewController
{
    NSUserDefaults *_defaults;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _defaults = [NSUserDefaults standardUserDefaults];
    
    _back.tintColor = APPCOLOR;
    _NavBar.titleTextAttributes = @{NSForegroundColorAttributeName: APPCOLOR, NSFontAttributeName : [UIFont systemFontOfSize:20]};
}

- (IBAction)back:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

//下一步(登录)
- (IBAction)next:(id)sender {
    NSLog(@"下一步");
    
    //网络请求
    [self network];
}
//用户使用协议
- (IBAction)Protocol:(id)sender {
    NSLog(@"用户使用协议");
}
//注册商城
- (IBAction)jumpToRegister:(id)sender {
    RegisterViewController *reg = [[RegisterViewController alloc] init];
    [self.navigationController pushViewController:reg animated:YES];
}
//忘记密码
- (IBAction)forgetPassword:(id)sender {
    NSLog(@"忘记密码");
}
//用户使用协议
- (IBAction)userUseProtocol:(id)sender {
    NSLog(@"用户使用协议");
}



//收起键盘
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [_UserName resignFirstResponder];
    [_password resignFirstResponder];
}

- (void)network{
    
    [_defaults setObject:_password.text forKey:@"Password"];
    
    NSDictionary *para = @{
                           @"memId"     : @"0",
                           @"loginname" : _UserName.text,
                           @"password"  : [_password.text MD5Hash],
                           @"Uid"       : @""
                           };
    
    [NetWorkManager requestWithType:HttpRequestTypeGet withMethodName:@"7494/api/Member/GetMember?" withParaments:para withSuccessBlock:^(NSDictionary *object) {
        NSLog(@"登录 : %@", object);
        NSLog(@"%@", object[@"Message"]);
        NSLog(@"%@", object[@"SubMessage"]);
        if ([object[@"Code"] isEqualToString:@"0"]) {
            //将Id存入沙盒
            [_defaults setObject:object[@"Rows"][@"UserName"] forKey:@"UserName"];
            NSLog(@"UserName  : %@", [_defaults objectForKey:@"UserName"]);
            [_defaults setObject:object[@"Rows"][@"Id"] forKey:@"Id"];
            [_defaults setObject:object[@"Rows"][@"UserName"] forKey:@"UserName"];
            NSLog(@"Id        :  %@", [_defaults objectForKey:@"Id"]);
            [EBUtility prompt:@"登陆成功" with:self];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }else{
            [EBUtility prompt:@"请确认输入的登录信息" with:self];
        }
        
    } withFailureBlock:^(NSError *error) {
        NSLog(@"登录网络请求失败 : %@", error);
    } progress:nil];
    
}

@end
